// pages/addlist/addlist.js
const app = getApp();
const url = app.globalData.url;
const common = require("../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ck:1,
    loding:true,
    showModalStatus: false,
    zdday:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (o) {
    console.log(o)
    if(o.ck){
      this.setData({ck:o.ck})
    }
  },
  dhbtn(e) {
    this.setData({ ck: e.currentTarget.dataset.ck });
    this.hqtxlist();
  },
  // 跳转url
  tourl(e) {
    let url = e.currentTarget.dataset.url;
    this.setData({ showModalStatus:false,it:{}})
    wx.navigateTo({
      url: url
    })
  },
  // 配置文件
  peizi: function () {
    let that = this;
    app.fetch('api/wx/getpeizi', { dlx: 'xcxadd' }, "GET", (err, data) => {
      console.log(data)
      if (data.code == 200) {
        that.setData({
          peizi: data.data
        });
        // article_content: WxParse.wxParse('article', 'html', data.data.xcxvip.tqsm.txt, that, 0);
        that.hquser();
      } else {
        wx.showModal({
          title: '接口数据错误',
          content: '接口:"' + url + 'api/wx/getpeizi.php' + '",msg:' + data.msg,
          showCancel: false,//是否显示取消按钮
          confirmText: "好的",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {

          },
          fail: function (res) { },//接口调用失败的回调函数
          complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
        })
      }
    })
  },
  // 获取user信息;
  hquser() {
    let that = this;
    wx.getStorage({
      key: 'openid',
      success(res) {
        app.fetch('api/wx/user', { openid: res.data }, "POST", (err, data) => {
          if (data.code == 200) {
            data.data[0].viptime = common.format(data.data[0].viptime);
            that.setData({ user: data.data[0] })
            // that.getcsData();
            that.hqtxlist();
          }
        })
      }
    })
  },
  hqtxlist(sx) {
    let that = this;
    let list = [];
    let pno = 1;
    if (sx) {
      list = that.data.list;
      pno = that.data.pno;
    }
    let sj = {
      uid: that.data.user.uid,
      ck: that.data.ck,
      pno: pno
    }
    app.fetch('api/wx/getaddlist', sj, "POST", (err, data) => {
      console.log(data);
      for (var i = 0; i < data.data.length; i++) {
        if (data.data[i].ewm) {
          data.data[i].ewm = url + data.data[i].ewm;
        }
        data.data[i].addtime = common.bijiao(data.data[i].addtime);
        data.data[i].zdt = common.format(data.data[i].zdtime);
        if (data.data[i].logolist) {
          var arr = data.data[i].logolist.split(",");
          data.data[i].logolist = [];
          let ls = arr.length < 5 ? arr.length : 5;
          for (let a = 0; a < ls; a++) {
            data.data[i].logolist.push(url + arr[a]);
          }
        } else {
          data.data[i].logolist = [];
          // data[i].imgs.push(data[i].tx);
        };
        list.push(data.data[i]);
      }
      let pp = Number(data.pno) + 1;
      that.setData({
        list: list,
        count: data.recordCount,
        pno: pp,
        pcount: data.pageCount,
        loding: false
      });
      wx.hideLoading();
    })
  },
  showModal: function (e) {
    // 显示遮罩层
    let it = e.currentTarget.dataset.it;
    this.setData({ it: it });
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function (sx) {
    // 隐藏遮罩层
    if(sx){
      this.setData({ it: {} });
    }
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    animation.translateY(300).step()
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },
  isdel() {
    let that = this;
    let del=1;
    if(that.data.it.isdel==1){
      del=2;
    }
    let sj = {
      did: that.data.it.did,
      isdel: del,
      uid: that.data.user.uid,
      uname: that.data.user.uname ? that.data.user.uname : that.data.user.nickName,
      tt: that.data.it.title,
      lx: "小程序端,发布列表处",
    };
    app.fetch('api/wx/upxq', sj, "POST", (err, data) => {
      console.log(data)
      if (data.code == 200) {
        wx.showToast({
          title: "操作成功",
          icon: 'success',
          duration: 1000
        });
        setTimeout(function () {
          that.hideModal("ss");
          that.hquser();
          that.setData({ it: {}});
        }, 1000)
      } else {
        wx.showModal({
          title: '接口数据错误',
          content: '接口:"' + url + 'api/wx/upxq' + '",msg:' + data.msg,
          showCancel: false,//是否显示取消按钮
          confirmText: "好的",//默认是“确定”
          confirmColor: 'skyblue',//确定文字的颜色
          success: function (res) {
            wx.navigateBack({
              data: 1,
              fail() {
                wx.redirectTo({
                  url: '/pages/index/index'
                })
              }
            })
          },
        })
      }
    })
  },
  szzd(){
    let that=this;
    that.hideModal();
    that.setData({zds:true})
  },
  pdt() {
    let that=this;
    if (this.data.peizi.xcx.fhyaddsf.txt == 1 || this.data.user.vip == 2) {
    } else {
      that.setData({ "zdday": 0 });
      wx.showModal({
        title: "提示",
        content: "非会员无法设置,请开通会员后设置",
        showCancel: true,//是否显示取消按钮
        cancelText: "等一会",//默认是“确定”
        confirmText: "开会员",//默认是“确定”
        success: function (res) {
          if (res.cancel) {
            //点击取消,默认隐藏弹框
            // if (options.finish) {
            that.setData({ zds: false})
            // }
          } else {
              wx.redirectTo({
                url: "/pages/vip/vip"
              })
          }
        },
        fail: function (res) { },//接口调用失败的回调函数
        complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
      })
    }
  },
  // 置顶设置
  zdsz(e) {
    if (this.data.peizi.xcx.fhyaddsf.txt == 1 || this.data.user.vip == 2) {
      this.setData({ "zdday": e.detail.value });
    } else {
      this.setData({ "zdday": 0 });
    }
  },
  qdzd(){
    let that=this;
    let zdjb = (that.data.zdday * that.data.peizi.xcxadd.zdjb.txt);
    if(that.data.user.vip==2){
       zdjb = (that.data.zdday * that.data.peizi.xcxadd.zdjb.txt * that.data.peizi.xcxadd.zdvipzk.txt);
    }
    let sj = {
      did: that.data.it.did,
      zdday: that.data.zdday,
      zdtime:that.data.it.iszd==1?that.data.it.zdtime:""
    };
    if(that.data.user.jb-zdjb>=0){
      var tt = '置顶内容消耗了' + zdjb + "个" + that.data.peizi.xcx.jb.txt;
      that.czjb(2, zdjb, tt, "置顶内容消耗了");
      app.fetch('api/wx/upxq', sj, "POST", (err, data) => {
        console.log(data)
        if (data.code == 200) {
          wx.showToast({
            title: "操作成功",
            icon: 'success',
            duration: 1000
          });
          setTimeout(function () {
            that.hqtxlist();
            that.setData({ it: {},zds:false });
          }, 1000)
        } else {
          wx.showModal({
            title: '接口数据错误',
            content: '接口:"' + url + 'api/wx/upxq' + '",msg:' + data.msg,
            showCancel: false,//是否显示取消按钮
            confirmText: "好的",//默认是“确定”
            confirmColor: 'skyblue',//确定文字的颜色
            success: function (res) {
              wx.navigateBack({
                data: 1,
                fail() {
                  wx.redirectTo({
                    url: '/pages/index/index'
                  })
                }
              })
            },
          })
        }
      })
    }else{
      wx.showModal({
        title: '提示:当前 ' + that.data.peizi.xcx.jb.txt +' 不足',
        content: "当前 " + that.data.peizi.xcx.jb.txt + ' 有: ' + that.data.user.jb+";置顶需要: "+zdjb+" 个",
        success(res) {
        }
      })
    }
  },
  thatqx(){
    this.setData({zds:false,it:{}});
  },
  czjb(sz, jb, tt, xlx) {
    let that = this;
    let sj = {
      uid: that.data.user.uid,
      sz: sz,
      jb: jb,
      tt: tt,
      lx: 'xcx',
      xlx: xlx ? xlx : ''
    };
    app.fetch('api/wx/czjb', sj, "POST", (err, data) => {
      console.log(data)
      if (data.code == 200) {
        // that.hquser();
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.peizi();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var count = that.data.pcount;
    var pno = that.data.pno;
    console.log(pno, count)
    if (pno - 1 < count) {
      wx.showLoading({
        title: "正在加载中....",
        // mask: true
      });
      // that.hqlist('sx');
      that.hqtxlist('sx');
    } else {
      that.setData({ jzs: true });
      setTimeout(function () {
        that.setData({ jzs: false });
      }, 3000)
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})